package com.multimedia.room;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class OralFileUtils {

    private static final String INTERNAL_CONFIG_FILE_NAME = "oral_record.xml";
    private static SharedPreferences mSharedPreferences;
    private static final OralFileUtils INSTANCE = new OralFileUtils();
    private static Context sContext;

    public static synchronized OralFileUtils getInstance(Context context) {
        sContext = context;
        mSharedPreferences = sContext.getSharedPreferences(
                INTERNAL_CONFIG_FILE_NAME, Context.MODE_PRIVATE);
        return INSTANCE;
    }

    public static void reStoreValueIntoSharePreferences(String key, Boolean value) {
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public ArrayList<FileStatus> getAll() {
        ArrayList<FileStatus> fileList = new ArrayList<FileStatus>();
        final Map<String, ?> keysMap = mSharedPreferences.getAll();
        Set<String> keySet = keysMap.keySet();//先获取map集合的所有键的Set集合
        Iterator<String> it = keySet.iterator();//有了Set集合，就可以获取其迭代器。

        while (it.hasNext()) {
            String key = it.next();
            Boolean value = (Boolean) keysMap.get(key);//有了键可以通过map集合的get方法获取其对应的值。
            fileList.add(new FileStatus(key, value));
            System.out.println("key: " + key + "-->value: " + value);//获得key和value值
        }
        return fileList;
    }

    public static class FileStatus {
       public String name;
       public boolean isUpload;

        public FileStatus(String path, boolean isUpload) {
            this.name = path;
            this.isUpload = isUpload;
        }
    }

}
